package edu.mtu.citizenscience.cs.utils;

import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Holds utilities for interacting with services
 */
public class Services {
	// reference var for passing messengers between services
	public static final String MESSENGER = "MESSENGER";

	/**
	 * Send a message from a service
	 * 
	 * @param   serviceName - String - the name of the service sending the message
	 * @param   messenger - Messenger - the messenger to send the message over
	 * @param   status - int - the service status
	 * @param   bundle - Bundle - the bundle to send with the message
	 * @return	void
	 */
	public static void sendMessage(String serviceName, Messenger messenger, int status, Bundle bundle)
	{
		// create a message
		Message backMsg = Message.obtain();
		// set the message status
	  	backMsg.arg1 = status;
	  	// attach the message data bundle
	  	backMsg.setData(bundle);
	  	
	  	// make sure the messenger points somewhere
	  	if (messenger != null)
	  	{
		  	try 
		  	{
		  		// send the message over the messenger
		  		messenger.send(backMsg);
		  	} 
		  	catch (RemoteException e) 
		  	{
		  		// log problem sending the message
		  		Log.w(serviceName, "Exception sending message", e);
		  	}
	  	}
	}
}
